package ace.cmp.spring.data.jdbc.test.dao.repository;

import ace.cmp.spring.data.jdbc.test.dao.entity.CASUser;
import ace.cmp.spring.data.jdbc.test.dao.entity.CASUserOnlyNameView;
import ace.cmp.spring.data.jdbc.test.dao.mapper.CASUserMapper;
import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jdbc.repository.query.Modifying;
import org.springframework.data.jdbc.repository.query.Query;
import org.springframework.data.repository.ListCrudRepository;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author caspar
 * @date 2023/2/13 9:51
 */
@Repository
public interface CASUserRepository
    extends ListCrudRepository<CASUser, String>,
        PagingAndSortingRepository<CASUser, String>,
        CASUserRepositoryEx,
        CASUserRepositoryQueryMEx {
  List<CASUser> findByNameContains(@Param("name") String name);

  List<CASUserOnlyNameView> findOnlyNameViewByNameContains(@Param("name") String name);

  @Transactional
  @Modifying
  @Query("DELETE FROM cas_user WHERE name LIKE CONCAT('%',:name,'%')")
  Integer deleteAllByNameContains(@Param("name") String name);

  Page<CASUser> findByName(@Param("name") String name, Pageable pageable);

  List<CASUser> findByNameContainsAndAndId(String name, String id);

  @Query(
      "SELECT * FROM cas_user WHERE mobile LIKE CONCAT('%',:mobile,'%') LIMIT "
          + CASUserMapper.FIND_ALL_BY_MOBILE_LIKE_LIMIT)
  List<CASUser> findByMobileLike(@Param("mobile") String mobile);
}
